using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Linq;

namespace AndyPike.Commons.Persistence.NHibernate
{
    public class NHibernateRepository<T> : IRepository<T> where T : class, IEntity
    {
        private readonly INHibernateSessionLocater sessionLocater;

        public NHibernateRepository(INHibernateSessionLocater sessionLocater)
        {
            GuardAgainst.ArgumentNull(sessionLocater, "sessionLocater");

            this.sessionLocater = sessionLocater;
        }

        public T FindById(Guid id)
        {
            GuardAgainst.ArgumentEmpty(id, "id");

            return sessionLocater.CurrentSession.Load<T>(id);
        }

        public void Save(T entity)
        {
            GuardAgainst.ArgumentNull(entity, "entity");

            sessionLocater.CurrentSession.Save(entity);
        }

        public IQueryable<T> AsQueryable()
        {
            return sessionLocater.CurrentSession.Linq<T>().AsQueryable();
        }

        public ICollection<T> Find(IQueryCommand<T> queryCommand)
        {
            GuardAgainst.ArgumentNull(queryCommand, "queryCommand");

            return queryCommand.Execute(this).ToList();
        }

        public T FindOne(IQueryCommand<T> queryCommand)
        {
            GuardAgainst.ArgumentNull(queryCommand, "queryCommand");

            IList<T> entities = queryCommand.Execute(this).ToList();

            return entities.Count > 0 ? entities[0] : null;
        }
    }
}